package uit.cinemainfo;

import android.content.Context;
import android.util.Log;
import android.view.*;
import android.widget.*;

public class FilmListViewAdapter extends ArrayAdapter<Film>{
	private Context context;
	private int layoutResourceId;
	private Film[] data= null;
	
	public FilmListViewAdapter(Context context, int layoutResourceId, Film[] data){
		super(context, layoutResourceId, data);
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.data = data;
	}
	
	static class FilmHolder{
		public TextView NameFilm;
		public TextView Genre;
		public TextView VoteFilm;
		public RatingBar RatingFilm;
		public ImageView PosterFilm;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		View row = convertView;
		FilmHolder holder = new FilmHolder();
		try{
			if(row == null){
				LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				row = inflater.inflate(layoutResourceId, parent, false);
			}
			else{
				holder = (FilmHolder)row.getTag();
			}
			
			Film pro = data[position];
			if(pro != null){
				holder.NameFilm = (TextView)row.findViewById(R.id.NameFilm);
				holder.Genre = (TextView)row.findViewById(R.id.Genre);
				holder.VoteFilm = (TextView)row.findViewById(R.id.VoteFilm);
				holder.RatingFilm = (RatingBar)row.findViewById(R.id.RatingFilm);
				holder.PosterFilm = (ImageView)row.findViewById(R.id.PosterFilm);
				row.setTag(holder);
				
				holder.NameFilm.setText(pro.namefilm);
				holder.Genre.setText(pro.kind);
				holder.VoteFilm.setText(pro.numvote + " " + context.getString(R.string.vote));
				holder.RatingFilm.setRating((float)pro.score);
				if(pro.image.endsWith("hobbit")){holder.PosterFilm.setImageResource(R.drawable.hobbit);}
				if(pro.image.endsWith("cold")){holder.PosterFilm.setImageResource(R.drawable.cold);}
				if(pro.image.endsWith("impo")){holder.PosterFilm.setImageResource(R.drawable.impo);}
				if(pro.image.endsWith("mis")){holder.PosterFilm.setImageResource(R.drawable.mis);}
				if(pro.image.endsWith("pi")){holder.PosterFilm.setImageResource(R.drawable.pi);}
				this.notifyDataSetChanged();
			}
		} catch (Exception e){Log.d("My check in adapter", e.getMessage());}
		return row;
	}
}
